package com.yswee.mallnavi.activity.common;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.yswee.framework.manager.WindowManager;
import com.yswee.framework.utils.LogcatUtils;
import com.yswee.framework.utils.ViewInject;
import com.yswee.mallnavi.R;
import com.yswee.mallnavi.activity.base.BaseActivity;
import com.yswee.mallnavi.context.ContextConstant;
import com.yswee.mallnavi.entity.home.NavEntity;

public class PicDialogActivity extends BaseActivity {

    private static final int MSGWAHT_CLOSE = 0;
    private static final int MSGDELAY_CLOSE = 60 * 1000;

    @ViewInject
    protected ImageView imgpicture;
    @ViewInject
    protected View imgclose;
    @ViewInject
    protected View btnclose;

    private NavEntity mNavEntity;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSGWAHT_CLOSE:
                    finish();
                    break;
            }
        }
    };

    @Override
    protected boolean isDialogTheme() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picdialog);
    }

    @Override
    protected void onQueryArguments(Intent intent) {
        super.onQueryArguments(intent);
        mNavEntity = (NavEntity) intent.getSerializableExtra(ContextConstant.INTENT_DATA);
        if (mNavEntity == null) {
            showToastMessage(R.string.error_param);
            finish();
        }
    }

    @Override
    protected void onBindListener() {
        super.onBindListener();
        imgclose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        btnclose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void onApplyData() {
        super.onApplyData();
        Glide.with(this)
                .load(mNavEntity.description)
                .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        LogcatUtils.e("PicDialog", e);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        ViewGroup.LayoutParams params = imgpicture.getLayoutParams();
                        int vw = imgpicture.getWidth();
                        if (vw <= 0) {
                            vw = WindowManager.get().getScreenWidth() - (int) getResources().getDimension(R.dimen.dp_50) * 2;
                        }
                        float scale = (float) vw / (float) resource.getIntrinsicWidth();
                        int vh = Math.round(resource.getIntrinsicHeight() * scale);
                        params.height = vh;
                        params.width = vw;
                        imgpicture.setLayoutParams(params);
                        return false;
                    }
                })
                .apply(new RequestOptions()
                        .placeholder(R.drawable.default_image_x)
                        .error(R.drawable.default_image_x))
                .into(imgpicture);
        mHandler.sendEmptyMessageDelayed(MSGWAHT_CLOSE, MSGDELAY_CLOSE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }
}